package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.constant.Navigation;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.DialogFastBuildUtil.Builder;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ThemeUtil;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.activity.TaskCenterActivity;
import com.qire.manhua.databinding.ActivityTaskCenterBinding;
import com.qire.manhua.dialog.SignInTaskRulesDialog;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.TaskCenterViewModel;
import com.qire.manhua.model.viewModel.impl.TaskCenterViewModel.TaskItemViewModel;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class TaskCenterController extends EventController<TaskCenterActivity> implements AutoVariable {

    private TaskCenterViewModel viewModel;

    public TaskCenterController(@NonNull TaskCenterActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityTaskCenterBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setTaskCenterController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.taskCenterController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(TaskCenterViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void taskLayoutOnScrollChange(View view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        int alpha = Math.min(255, scrollY / 2);
        alpha = Math.max(0, alpha);
        viewModel.changeNavBarBgAlpha(alpha);

        if(alpha > 128) {
            ThemeUtil.changeStatusBarLightOrDark(owner, true, true);
        } else {
            ThemeUtil.changeStatusBarLightOrDark(owner, false, true);
        }
    }

    public void backBtnOnclick(View view){
        owner.onBackPressed();
    }


    public void signInRulesBtnOnClick(View view){
        new SignInTaskRulesDialog(owner).show();
    }

    public void taskBtnOnClick(View view) {
        TaskItemViewModel itemViewModel = (TaskItemViewModel) view.getTag();
        if(itemViewModel == null) return;

        if(itemViewModel.canReceive()) {
            itemViewModel.receiveReward();
        } else if(itemViewModel.unfinished()) {
            gotoComplete(itemViewModel.taskID());
        }
    }

    private void gotoComplete(int taskId) {
        switch (taskId) {
            case 100://阅读2部漫画
            case 102://为1个漫画章节点赞
                AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.HomePage)
                        .withData("tabIndex", PageTabIndex.TAB_INDEX_HOME_RECOMMEND)
                        .withData("subTabIndex", PageTabIndex.TAB_INDEX_RECOMMEND_HOT)
                        .go();
                break;
            case 101://分享1部漫画
            case 106://收藏3部自己喜欢的漫画
                AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.HomePage)
                        .withData("tabIndex", PageTabIndex.TAB_INDEX_HOME_CLASSIFICATION)
                        .go();
                break;
            case 112://单次金币充值大于6元
                AntsRouter.ROUTER.buildCard(Navigation.Page.BuyCoinsMarketPage).navigation();
                break;
            case 109://绑定手机
                AntsRouter.ROUTER.buildCard(Navigation.Page.AccountBindGuidePage).navigation();
                break;
            case 110://关注奇热漫画微信公众号
                if(UserEntity.self.isNotBindWeChat()) {
                    final Builder builder = DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_r);
                    builder.setTextView(R.id.dialog_title, "您还未绑定微信号\n\n将无法获得关注任务奖励")
                            .setTextVisible(R.id.dialog_content, true)
                            .setTextView(R.id.dialog_button_dark, "取消", view -> {
                                builder.dismiss();
                                view.setOnClickListener(null);
                            })
                            .setTextView(R.id.dialog_button_light, "立即绑定", view -> {
                                viewModel.bindWeChat();
                                builder.dismiss();
                                view.setOnClickListener(null);
                            })
                            .show();
                } else {
                    AntsRouter.ROUTER.buildCard(Navigation.Page.SubscribeWeChatPage).navigation();
                }
                break;
            case 111://下载奇热漫画APP
                break;
            case 104://发送1次弹幕
                AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.HomePage)
                        .withData("tabIndex", PageTabIndex.TAB_INDEX_HOME_RECOMMEND)
                        .go();
                break;
            case 105://完善个人信息
                AntsRouter.ROUTER.buildCard(Navigation.Page.UserProfileEditPage).navigation();
                break;
            case 107://离线缓存1个章节
                AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.HomePage)
                        .withData("tabIndex", PageTabIndex.TAB_INDEX_HOME_BOOKSHELF)
                        .withData("subTabIndex", PageTabIndex.TAB_INDEX_BOOKSHELF_DOWNLOAD)
                        .go();
                break;
        }
    }

}
